﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Reflection;

namespace Medianamik.UI.Lock
{
    public class LockRequester : WebControl, IScriptControl
    {
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                // Test for ScriptManager and register if it exists
                var sm = ScriptManager.GetCurrent(Page);
                if (sm == null)
                {
                    throw new Exception("A ScriptManager control must exist on the current page.");
                }
                sm.RegisterScriptControl(this);
                sm.Services.Add(new ServiceReference("/Medianamik/Services/LockWebService.asmx"));
            }

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (!this.DesignMode)
            {
                var sm = ScriptManager.GetCurrent(Page);
                sm.RegisterScriptDescriptors(this);
            }
            base.Render(writer);
        }


        #region IScriptControl Members

        public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
        {
            ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Medianamik.LockRequester", this.ClientID);           
            return new ScriptDescriptor[] { descriptor };
        }

        public IEnumerable<ScriptReference> GetScriptReferences()
        {
            ScriptReference reference = new ScriptReference();
            reference.Path = Medianamik.Core.Sugar.Resources.GetWebResourceUrl("Lock.lockRequester.js", this.Page, this.GetType(), Assembly.GetAssembly(this.GetType()));
            return new ScriptReference[] { reference };
        }

        #endregion
    }
}
